package com.example.shop_2208.detail

import android.R.attr.thumb
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.ActionBar.LayoutParams
import android.util.Log
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2208.R
import com.example.shop_2208.databinding.ActivityDetailBinding
import com.example.shop_2208.mvvm.BaseActivity
import com.example.shop_2208.utils.GlideUtils
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb


@Route(path = "/app/detail")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    @Autowired
    @JvmField
    var goodsId = 0

    override fun initData() {
        viewModel.detailSuccess.observe(this){
            if(it.code==200){
                binding.detail = it.data
            }
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //添加动画效果
                val image = ImageView(this)
                val param = LayoutParams(200,200)
                image.layoutParams = param  //图片的大小为200*200
                GlideUtils.loadImage(this,image,"https://img13.360buyimg.com/n7/jfs/t2590/338/4222589387/237425/25e40fb/57b12ac6N61374a75.jpg")
                binding.ll.addView(image)//将图片显示在布局上

                //设置动画效果
                //获取屏幕宽度和高度
                val width = windowManager.defaultDisplay.width
                val height = windowManager.defaultDisplay.height
                val translateX = ObjectAnimator.ofFloat(image,"translationX",0f,width.toFloat())
                val translateY = ObjectAnimator.ofFloat(image,"translationY",0f,-(height.toFloat()))
                val scaleX = ObjectAnimator.ofFloat(image,"scaleX",2f,0.5f)
                val scaleY = ObjectAnimator.ofFloat(image,"scaleY",2f,0.5f)

                val animatorSet = AnimatorSet()
                animatorSet.duration = 2000
                animatorSet.play(translateX).with(translateY).with(scaleX).with(scaleY)
                animatorSet.start()
                //动画结束后需要消失
                animatorSet.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {
                        //开始
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        //结束----完成了
                        binding.ll.removeView(image)
                    }

                    override fun onAnimationCancel(animation: Animator) {
                        //取消
                    }

                    override fun onAnimationRepeat(animation: Animator) {
                        //重复
                    }
                })

            }
        }

    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.detail(goodsId)

        //点击加入购物车
        binding.tvAddCar.setOnClickListener {
            viewModel.addCar(goodsId,1)
        }

        //点击返回图标，直接关闭当前界面
        binding.detailBack.setOnClickListener {
            finish()
        }
        //分享
        binding.detailShare.setOnClickListener {
            val web: UMWeb = UMWeb("http://10.161.9.80:7012/goods/detail?goods_id="+goodsId)
            web.title = "分享商品" //标题
            val image: UMImage = UMImage(this@DetailActivity, "https://img13.360buyimg.com/n7/jfs/t2590/338/4222589387/237425/25e40fb/57b12ac6N61374a75.jpg") //网络图片
            web.setThumb(image) //缩略图
            web.description = "这是一个商品图片" //描述
            //真正去分享
            ShareAction(this)
                .withMedia(web)
                .setDisplayList(SHARE_MEDIA.QQ)//设置分享平台
                    //设置回调事件--主要目的 分享失败时查看原因
                .setCallback(object :UMShareListener{
                    override fun onStart(p0: SHARE_MEDIA?) {

                    }

                    override fun onResult(p0: SHARE_MEDIA?) {

                    }

                    override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                        Log.d("sss", "onError: ${p1?.message}")
                    }

                    override fun onCancel(p0: SHARE_MEDIA?) {

                    }
                })
                .open()
        }
        //点击商品编码生成二维码
        binding.tvCode.setOnClickListener {
            val procuctId = "123456"//这只是一个例子，并不是真实的值
            val barcodeEncoder = BarcodeEncoder()
            //val bitmip = barcodeEncoder.encodeBitmap(procuctId,BarcodeFormat.CODE_128,500,200)//条形码
            val bitmip = barcodeEncoder.encodeBitmap(procuctId,BarcodeFormat.QR_CODE,500,500)//二维码
            binding.detailCode.setImageBitmap(bitmip)
        }


    }

    override fun getViewModel(): Class<DetailViewModel> = DetailViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_detail

}